package com.cn.esermis.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.esermis.dpld.entity.DataAcquisitionInstrument;
import com.cn.esermis.dpld.entity.PreMarking;
import com.cn.esermis.dpld.model.AjaxResult;
import com.cn.esermis.dpld.model.PreMarkingVo;
import com.cn.esermis.dpld.service.IDataAcquisitionInstrumentService;
import com.cn.esermis.dpld.service.IPreMarkingService;
import com.cn.esermis.model.BaseUser;
import com.cn.esermis.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.cn.esermis.dpld.model.AjaxResult.error;
import static com.cn.esermis.utils.TokenUtils.extractToken;

@RestController
@RequestMapping("/pre_marking")
public class PreMarkingController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private IDataAcquisitionInstrumentService dataAcquisitionInstrumentService;
    @Resource
    private IPreMarkingService preMarkingService;

    @PostMapping("/add")
    public Object add(@RequestBody PreMarking preMarking, HttpServletRequest request) {
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        assert userInfo != null;
        preMarking.setCompanyCode(userInfo.getCompanyId());
        preMarking.setDepartmentCode(userInfo.getFactoryId());
        preMarkingService.save(preMarking);
        DataAcquisitionInstrument dao =  new DataAcquisitionInstrument();
        dao.setId(preMarking.getDataid());
        dao.setWorkCondition(preMarking.getWorkingCondition());
        dataAcquisitionInstrumentService.updateById(dao);
        sendMessage();
        return AjaxResult.success("success");
    }

    @GetMapping("/del")
    public Object del(String id) {
        preMarkingService.removeById(id);
        sendMessage();
        return AjaxResult.success("success");
    }

    @PostMapping("/edit")
    public Object edit(@RequestBody PreMarking preMarking, HttpServletRequest request) {
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        assert userInfo != null;
        preMarking.setCompanyCode(userInfo.getCompanyId());
        preMarking.setDepartmentCode(userInfo.getFactoryId());
        preMarkingService.updateById(preMarking);
        sendMessage();
        return AjaxResult.success("success");
    }

    @GetMapping("/getInfo")
    public Object getInfo(String id) {
        return AjaxResult.success(preMarkingService.getById(id));
    }

    @GetMapping("/list")
    public Object list(PreMarkingVo vo,HttpServletRequest request) {
        QueryWrapper<PreMarking> wrapper = new QueryWrapper<>();
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        if (userInfo == null) {
            return error("无数据");
        }
        wrapper.eq(userInfo.getCompanyId() != null, "company_code", userInfo.getCompanyId());
        wrapper.eq(userInfo.getFactoryId() != null, "department_code", userInfo.getFactoryId());
        IPage<PreMarking> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        sendMessage();
        return AjaxResult.success(preMarkingService.page(page,wrapper));
    }


    public void sendMessage() {
        List<PreMarking> preMarking = preMarkingService.getNewMarking();
        if (preMarking != null){
            preMarking.forEach(x->{
                redisTemplate.opsForValue().set(x.getMnCode(), JSON.toJSONString(x));
            });
        }
    }

}
